﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Prodma.DataAccess.DataService;
using Prodma.DataAccess.DataService.ListService;
using System.Web.UI.WebControls;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Models;

namespace Prodma.Panel.Controllers
{
    public class UrunlerController : Controller
    {

        #region Repository Setters

        private UrunlerService service = new UrunlerService();
        private IList<UrunlerVm> urunlerList;


        public UrunlerController()
        {
        }

        #endregion

        #region Index()

        public ActionResult Index()
        {
            string a = Request.Form["Kategori"];
            string pageSize = Request.Params.Get("ps");
            String page = Request.Params.Get("p");
            int p=1, ps=10;
            if(pageSize!=null)
                ps=Convert.ToInt32(pageSize);

            if(page!=null)
                p=Convert.ToInt32(page);

            IList<UrunlerVm> list = null;
            LoginUser user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            List<SelectList> selectedList = new List<SelectList>();
            selectedList.Add(ProductCategoryList.GetList());
            selectedList.Add(KategoriList.GetList());
            ViewData["selectedList"] = selectedList;
            ViewData["TotalRowCount"] = service.GetTotalRowCount();
            SepetService sepetService = new SepetService();
            ViewData["Sepet"] = sepetService.GetList(user.Id);
            FilterParametersService filterService = new FilterParametersService();
            tb_FilterParameters parameters = filterService.GetFilterParameters(user.Id);

            if (parameters != null)
            {
                UrunlerVm urun = new UrunlerVm();
                if (parameters.KategoriID != null)
                    urun.KategoriID = parameters.KategoriID;
                if (parameters.MarkaID != null)
                    urun.MarkaArabaID = parameters.MarkaID;
                if (parameters.ModelID != null)
                    urun.ModelID = parameters.ModelID;

                list = service.GetListDynamic(urun);
            }
            else
                list = service.GetList();

            

            int totalPage = list.Count / ps;
            if(list.Count%ps!=0) 
                totalPage++;


            ViewData.Add("ListLenght", totalPage);

            if (ps != -1)
            {
                list = list.Skip((p-1) * ps).Take(ps).ToList();
            }
            return View(list);
            
        }

        public ActionResult GetListDynamic()
        {
            string kategori = Request.Form["Kategori"];
            string marka = Request.Form["Marka"];
            string model = Request.Form["Model"];
            string radio = Request.Form["Radio"];

            UrunlerVm urun = new UrunlerVm();
            if (kategori == "" && marka == "" && (model == "" || Convert.ToInt32(model) == 0))
                urun = null;
            else
            {
                if (kategori != "")
                {
                    urun.KategoriID = Convert.ToInt32(kategori);
                }
                if (marka != "")
                {
                    urun.MarkaArabaID = Convert.ToInt32(marka);
                }
                if (model != "" && Convert.ToInt32(model) != 0)
                {
                    urun.ModelID = Convert.ToInt32(model);
                }
                if (radio != "0")
                {
                    urun.StokDurumID = Convert.ToInt32(radio);
                }
            }
            return PartialView("/views/shared/userControls/UrunlerList.ascx", service.GetListDynamic(urun));
        }

        public ActionResult GetListDynamicByTextBoxes()
        {
            //string orijinalKod = Request.Form["OrijinalKod"];
            string stokKod = Request.Form["StokKod"];
            string stokAd = Request.Form["StokAd"];
            string radio = Request.Form["Radio"];

            Dictionary<string, string> parameters = new Dictionary<string, string>();

            
            if (stokKod != "")
                parameters.Add("StokKod", stokKod);
            if (stokAd != "")
                parameters.Add("StokAd", stokAd);
            if (radio != "0")
                parameters.Add("StokDurumID", radio);

            return PartialView("/views/shared/userControls/UrunlerList.ascx", service.GetListDynamicByTextBoxes(Prodma.DataAccess.Helper.LinqExpressions.QueryOlustur.Urunler(parameters)));
            

        }

        public ActionResult GetProductByAlternatifKod(string alternatifKod)
        {
            LoginUser user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            UrunlerVm product = new UrunlerVm();
            
            product = service.GetProductByAlternatifKod(alternatifKod,user.CariID);
            if (product != null)
                return View("/views/Urunler/ProductDetail.aspx", product);
            else
                return RedirectToAction("Index", "Urunler");
        }

        public ActionResult GetProductByOrijinalKod(string orijinalKod)
        {
            UrunlerVm product = new UrunlerVm();
            product = service.GetProductByOrijinalKod(orijinalKod);
            if (product != null)
                return View("/views/Urunler/ProductDetail.aspx", product);
            else
                return RedirectToAction("Index", "Urunler");
        }
        #endregion

        #region ProductDetail
        public ActionResult ProductDetail(int id)
        {
            //int id = Convert.ToInt32(Request.Form["id"]);
            return View("/views/Urunler/ProductDetail.aspx", service.GetProduct(id));
        }
        #endregion

       


        public ActionResult SetDefaultBrand()
        {
            string a = Request.QueryString["productCategoryBrandId"].ToString();
            if (Request.QueryString["productCategoryBrandId"] != "" && Request.QueryString["productCategoryBrandId"] != null)
            {
                int brandId = Convert.ToInt32(Request.QueryString["productCategoryBrandId"]);
                
                return RedirectToAction("index/?brandId=" + brandId);
            }
            else return RedirectToAction("index");
        }


        public ActionResult GetResult()
        {
                        
            int a = Convert.ToInt32(Request.QueryString["id"]);
            //return this.Json(ProductCategoryList.GetList(a));
            var sdfkj = ProductCategoryList.GetList();

            var list = new List<SelectListItem> {new SelectListItem {Text = "not selected", Value = ""}};
          
            list.AddRange(sdfkj.Select(o => new SelectListItem
            {
                Text = o.Text ,
                Value = o.Value,
            }));
            //return Json(list);


            return Json(new[] {                
            new { Value = 1, Text = "value 1" },
            new { Value = 2, Text = "value 2" },
            new { Value = 3, Text = "value 3" },
        }, JsonRequestBehavior.AllowGet);


        }

        public JsonResult GetModels(int markaId)
        {
            return Json(ProductCategoryList.GetModels(markaId), JsonRequestBehavior.AllowGet);
        }
        public class SelectOption
        {
            public int Value { get; set; }
            public String Text { get; set; }
        }
        

    }
}
